//
//  DestinationViewController.m
//  hai游～
//
//  Created by jakf_17 on 15/5/21.
//  Copyright (c) 2015年 jakf_17. All rights reserved.
//

#import "DestinationViewController.h"
#import "CountryDetailViewController.h"
#import "CityDetailViewController.h"
#import "SpotViewController.h"
#import "DestinationTableViewCell.h"
#import "DestinationTitleModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface DestinationViewController ()<UITableViewDataSource, UITableViewDelegate>


@end

@implementation DestinationViewController


- (void)dealloc
{
    
    [_tableView release];
    [_array release];
    [super dealloc];
    
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    [self loadDataSource];
    [self createMyTableView];
}

- (void)createMyTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 1) {
        
    
   
    DestinationTitleModel *titilModel = _array[indexPath.row];
    NSArray *array = titilModel.array;
    if (array.count > 2) {
        return  ([[UIScreen mainScreen] bounds].size.width - 30)  + 10 + 5;
    }
    return ([[UIScreen mainScreen] bounds].size.width - 30) / 2 + 5;
    } else {
        return 30;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *stringCell= @"stringCell";
    static NSString *destinationCell = @"destinationCell";
    static NSString *smallDestinationCell = @"smallDestinationCell";
    if ([_array[indexPath.row] isKindOfClass:[NSString class]]) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:stringCell];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:stringCell];
        }
        cell.textLabel.text = _array[indexPath.row];
        DestinationTitleModel *title = _array[indexPath.row + 1];
        if (title.more.boolValue == true) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }    else {
        
        
        DestinationTitleModel *titleModel = _array[indexPath.row];
        if (titleModel.array.count <= 2) {
            DestinationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:smallDestinationCell];
            

            if (cell == nil) {
                cell = [[[DestinationTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:smallDestinationCell] autorelease];
                [cell createMyCellWithArray:titleModel.array];
                cell.block = ^(DestinationItemModel *model){
                    if (model.type.integerValue == 5) {
                        SpotViewController *spot = [[SpotViewController alloc] init];
                        spot.model = model;
                        [self.navigationController pushViewController:spot animated:YES];
                    } else if (model.type.integerValue == 3 || model.index.integerValue == 4) {
                        CityDetailViewController *city = [[CityDetailViewController alloc] init];
                        city.model = model;
                        [self.navigationController pushViewController:city animated:YES];
                    } else {
                    CountryDetailViewController *countryDetail = [[CountryDetailViewController alloc] init];
                    countryDetail.model = model;
                    [self.navigationController pushViewController:countryDetail animated:YES];
                }
            };
            }
            if (titleModel.array.count >= 1) {
                
                DestinationItemModel *model = titleModel.array[0];
                [cell.leftUpImage sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"1.gif"]];
                cell.leftUpImage.model = model;
                if (titleModel.array.count >= 2) {
                    
                    DestinationItemModel *model2 = titleModel.array[1];
                    [cell.rightUpImage sd_setImageWithURL:[NSURL URLWithString:model2.cover] placeholderImage:[UIImage imageNamed:@"1.gif"]];
                    cell.rightUpImage.model = model2;
                }
            }
            return cell;
        } else {
        DestinationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:destinationCell];
        if (cell == nil) {
            cell = [[[DestinationTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:destinationCell] autorelease];
            [cell createMyCellWithArray:titleModel.array];
            cell.block = ^(DestinationItemModel *model){
                
                if (model.type.integerValue == 5) {
                    SpotViewController *spot = [[SpotViewController alloc] init];
                    spot.model = model;
                    [self.navigationController pushViewController:spot animated:YES];
                } else if (model.type.integerValue == 3 || model.index.integerValue == 4 || [model.name isEqualToString:@"新加坡"]) {
                    CityDetailViewController *city = [[CityDetailViewController alloc] init];
                    city.model = model;
                    [self.navigationController pushViewController:city animated:YES];
                } else {
                CountryDetailViewController *countryDetail = [[CountryDetailViewController alloc] init];
                countryDetail.model = model;
                [self.navigationController pushViewController:countryDetail animated:YES];
            }

            
        };
        }
        
        
        
        
        
        if (titleModel.array.count >= 1) {
            
            DestinationItemModel *model = titleModel.array[0];
            [cell.leftUpImage sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"1.gif"]];
            cell.leftUpImage.model = model;
            cell.leftUpImage.label.text = model.name;
            if (titleModel.array.count >= 2) {
                
                DestinationItemModel *model2 = titleModel.array[1];
                [cell.rightUpImage sd_setImageWithURL:[NSURL URLWithString:model2.cover] placeholderImage:[UIImage imageNamed:@"1.gif"]];
                cell.rightUpImage.model = model2;
                cell.rightUpImage.label.text = model2.name;
                if (titleModel.array.count >= 3) {
                    
                    DestinationItemModel *model3 = titleModel.array[2];
                    [cell.leftDownImage sd_setImageWithURL:[NSURL URLWithString:model3.cover] placeholderImage:[UIImage imageNamed:@"1.gif"]];
                    cell.leftDownImage.model = model3;
                    cell.leftDownImage.label.text = model3.name;
                    if (titleModel.array.count >= 4) {
                        
                        DestinationItemModel *model4 = titleModel.array[3];
                        [cell.rightDownImage sd_setImageWithURL:[NSURL URLWithString:model4.cover] placeholderImage:[UIImage imageNamed:@"1.gif"]];
                        cell.rightDownImage.model = model4;
                        cell.rightDownImage.label.text = model4.name;
                    }
                }
            }
        }
        return cell;
        }
    }
    
    
}
- (void)loadDataSource {
    // Request
    NSString *URLPath = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/v3/?last_modified_since=0"];
    NSURL *URL = [NSURL URLWithString:URLPath];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        
            NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *array = [res objectForKey:@"elements"];
            for (int i = 0; i < array.count ; i++) {
                NSDictionary *subDic = array[i];
                DestinationTitleModel *titleModel = [[DestinationTitleModel alloc] init];
                [titleModel setValuesForKeysWithDictionary:subDic];
                NSArray *subArray = [subDic objectForKey:@"data"];
                for (int j = 0; j < subArray.count; j++) {
                    NSDictionary *theDic = subArray[j];
                    
                    DestinationItemModel *model = [[DestinationItemModel alloc] init];
                    [model setValuesForKeysWithDictionary:theDic];
                    model.index = titleModel.index;
                    [titleModel.array addObject:model];
                    
                }
                [_array addObject:titleModel.title];
                [_array addObject:titleModel];
            }
        [_tableView reloadData];
        
    }];
}



@end
